<script>
export default {
  name: "User",
  data() {
    return {
      Authorities: localStorage.getItem("authorities") ? JSON.parse(localStorage.getItem("authorities")) : {},
      tableData: [],
      total: 0,
      pageNum: 1,
      pageSize: 5,
      username: "",
      nickname: "",
      email: "",
      phone: "",
      address: "",
      user: {},
      dialogFormVisible: false,
      deleted_id: 0,
      ifSelecting: false,
      EditDisabled: false,
      DeleteDisabled: false,
    }
  },
  created(){
    this.firstLoad();
    console.log(this.Authorities);
    this.EditDisabled = this.Authorities.Authority5;
    this.DeleteDisabled = this.Authorities.Authority4;
  },
  methods: {
    firstLoad() {
      this.request.get("http://localhost:9090/user/all", {
        params: {
          pageNum: this.pageNum,
          pageSize: this.pageSize
        }
      }).then(res => {
        console.log(res);
        this.tableData = res.data;
        this.total = res.total;
      })
    },
    load() {
      this.request.get("http://localhost:9090/user/page",  {
        params: {
          pageNum: this.pageNum,
          pageSize: this.pageSize,
          username: this.username,
          nickname: this.nickname,
          email: this.email,
          phone: this.phone,
          address: this.address
        }
      }).then(res => {
        console.log(res);
        this.tableData = res.data;
        this.total = res.total;
      })
      this.ifSelecting = true;
      // fetch("http://localhost:9090/user/page?pageNum=1" +
      //     "&pageSize=" +
      //     this.pageSize + "&username=" +
      //     this.username + "&nickname=" +
      //     this.nickname + "&email=" +
      //     this.email + "&phone=" +
      //     this.phone + "&address=" +
      //     this.address)
      //     .then(res => res.json()).then(res => {
      //   console.log(res)
      //   this.tableData = res.data
      //   this.total = res.total
      // })
    },
    save() {
      this.request.post("http://localhost:9090/user/save", this.user).then(res => {
        if (res) {
          this.$message.success("保存成功");
          this.dialogFormVisible = false;
          this.firstLoad();
        } else {
          this.$message.error("保存失败");
        }
      })
    },
    handleAdd() {
      this.dialogFormVisible = true;
      this.user = {};
    },
    handleEdit(row) {
      this.user = row;
      this.user = JSON.parse(JSON.stringify(row));
      this.dialogFormVisible = true;
    },
    handleDel(id) {
      this.request.delete("http://localhost:9090/user/" + id).then(res => {
        if (res) {
          this.$message.success("删除成功");
          this.firstLoad();
        } else {
          this.$message.error("删除失败");
        }
      })
    },
    reset(){
      this.pageNum = 1;
      this.username = "";
      this.nickname = "";
      this.email = "";
      this.phone = "";
      this.address = "";
      this.ifSelecting = false;
      this.firstLoad();
    },
    handleSizeChange(pageSize){
      console.log(pageSize)
      this.pageSize = pageSize
      if (this.ifSelecting) {
        this.load()
      } else {
        this.firstLoad()
      }
    },
    handleCurrentChange(pageNum){
      console.log(pageNum)
      this.pageNum = pageNum
      if (this.ifSelecting) {
        this.load()
      } else {
        this.firstLoad()
      }
    }
  }
}
</script>

<template>
  <div>
    <div style="margin: 10px 0">
      <el-input style="width: 150px" placeholder="请输入名称" suffix-icon="el-icon-search" v-model="username" :disabled="!this.Authorities.Authority6"></el-input>
      <el-input style="width: 150px" placeholder="请输入昵称" suffix-icon="el-icon-search" class="ml-5" v-model="nickname" :disabled="!this.Authorities.Authority6"></el-input>
      <el-input style="width: 150px" placeholder="请输入邮箱" suffix-icon="el-icon-message" class="ml-5" v-model="email" :disabled="!this.Authorities.Authority6"></el-input>
      <el-input style="width: 150px" placeholder="请输入手机号" suffix-icon="el-icon-message" class="ml-5" v-model="phone" :disabled="!this.Authorities.Authority6"></el-input>
      <el-input style="width: 150px" placeholder="请输入地址" suffix-icon="el-icon-position" class="ml-5" v-model="address" :disabled="!this.Authorities.Authority6"></el-input>
      <el-button class="ml-5" type="primary" @click="load" :disabled="!this.Authorities.Authority6">搜索</el-button>
      <el-button type="warning" @click="reset" :disabled="!this.Authorities.Authority6">重置</el-button>
      <el-button class=“ml-5” type="primary" @click="handleAdd" :disabled="!this.Authorities.Authority3">新增<i class="el-icon-circle-plus-outline"></i></el-button>
    </div>
    <el-table :data="tableData" border stripe header-cell-class-name="headerBg">
      <el-table-column prop="id" label="ID" width="80"></el-table-column>
      <el-table-column prop="username" label="用户名" width="140"></el-table-column>
      <el-table-column prop="nickname" label="昵称" width="120"></el-table-column>
      <el-table-column prop="email" label="邮箱" width="120"></el-table-column>
      <el-table-column prop="phone" label="电话" width="120"></el-table-column>
      <el-table-column prop="address" label="地址"></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button type="success" @click="handleEdit(scope.row)" :disabled="!EditDisabled">编辑<i class="el-icon-edit"></i></el-button>
          <el-popconfirm
              class="ml-5"
              title="确定删除这条数据吗？"
              icon="el-icon-info"
              icon-color="red"
              confirm-button-text="确定"
              cancel-button-text="取消"
              @confirm="handleDel(scope.row.id)"
          >
            <el-button type="danger" slot="reference" :disabled="!DeleteDisabled">删除<i class="el-icon-remove-outline"></i></el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <div style="padding: 10px 0">
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageNum"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
    </div>
    <el-dialog title="用户信息" :visible.sync="dialogFormVisible" width="30%">
      <el-form label-width="100px">
        <el-form-item label="用户名">
          <el-input v-model="user.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="昵称">
          <el-input v-model="user.nickname" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="邮箱">
          <el-input v-model="user.email" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="电话">
          <el-input v-model="user.phone" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="地址">
          <el-input v-model="user.address" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<style>

</style>